home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 21 / CU Amiga Magazine's Super CD-ROM 21 (1998)(EMAP Images)(GB)[!][issue 1998-04].iso / CUCD / Magazine / C_Tutorial / Part-9 / wb0 / loadsave.c < prev    next >
C/C++ Source or Header  |  1998-02-01  |  4KB  |  145 lines

  1. #include "loadsave.h"
  2. #include "bitmap.h"
  3. #include "drawwin.h"
  4. #include "gui.h"
  5.  
  6. #include "iff.h"
  7.  
  8. #include<libraries/asl.h>
  9.  
  10. #include<string.h>
  11. #include<stdio.h>
  12.  
  13. #include<clib/asl_protos.h>
  14. #include<clib/dos_protos.h>
  15. #include<clib/graphics_protos.h>
  16.  
  17. /* The size of our filename string */
  18. #define MAXFILENAME        (300)
  19.  
  20. /* Global handles for our requesters */
  21. static struct FileRequester* loadreq = NULL;
  22. static struct FileRequester* savereq = NULL;
  23.  
  24. /* Open an ASL load file requester */
  25. int load()
  26. {
  27.     int going = TRUE;
  28.     /* Allocate the requester if we haven't already */
  29.     if(loadreq == NULL)
  30.         loadreq = (struct FileRequester*)
  31.             AllocAslRequestTags(ASL_FileRequest,
  32.                                                     ASLFR_TitleText,            "Load File",
  33.                                                     ASLFR_Flags1,                    FRF_DOPATTERNS,
  34.                                                     ASLFR_InitialPattern,    "#?.iff",
  35.                                                     TAG_DONE);
  36.     if(loadreq)
  37.     {
  38.         struct Window* win = getDrawWin();
  39.         if(AslRequestTags(loadreq, ASLFR_Window, win, TAG_DONE))
  40.         {
  41.             char filename[MAXFILENAME];
  42.             /* Create complete filename from ASL's dir and file */
  43.             strcpy(filename, loadreq->rf_Dir);
  44.             if(AddPart(filename, loadreq->rf_File, MAXFILENAME))
  45.             {
  46.                 IFFL_HANDLE handle;
  47.                 /* Try to open the IFF file */
  48.                 if(handle = IFFL_OpenIFF(filename, IFFL_MODE_READ))
  49.                 {
  50.                     UWORD colortable[256];
  51.                     /* Get colour information */
  52.                     LONG count = IFFL_GetColorTab(handle, colortable);
  53.                     /* Get display information */
  54.                     ULONG displayid = IFFL_GetViewModes(handle);
  55.                     /* Get picture information */
  56.                     struct IFFL_BMHD* bmhd = IFFL_GetBMHD(handle);
  57.                     /* Try to adjust the screen to fit */
  58.                     if(bmhd)
  59.                     {
  60.                         closeGUI();
  61.                         /* If we succeed then update our local win */
  62.                         openGUI(bmhd->nPlanes, bmhd->w, bmhd->h, displayid);
  63.                         win = getDrawWin();
  64.                     }
  65.                     if(win)
  66.                     {
  67.                         /* Change screen colours */
  68.                         LoadRGB4(&(win->WScreen->ViewPort), colortable, count);
  69.                         /* If we can load the picture, update window's display */
  70.                         if(IFFL_DecodePic(handle, getBitmap()))
  71.                             CopySBitMap(win->WLayer);
  72.                         else
  73.                             printf("Error: could not decode IFF picture\n");
  74.                     }
  75.                     else
  76.                         going = FALSE;  /* The only fatal error */
  77.                     IFFL_CloseIFF(handle);
  78.                 }
  79.                 else
  80.                     printf("Error: could not open IFF file\n");
  81.             }
  82.             else
  83.                 printf("Error: could not make filename\n");
  84.         }
  85.         /* else: requester was cancelled */
  86.     }
  87.     else
  88.         printf("Error: could not allocate ASL (load) file request\n");
  89.     return going;
  90. }
  91.  
  92. /* Open an ASL save file requester */
  93. void save()
  94. {
  95.     /* Another way of saying "allocate if we haven't already" */
  96.     if(savereq ||
  97.         (savereq = (struct FileRequester*)
  98.             AllocAslRequestTags(ASL_FileRequest,
  99.                                                     ASLFR_TitleText,            "Save File",
  100.                                                     ASLFR_Flags1,                    FRF_DOPATTERNS | FRF_DOSAVEMODE,
  101.                                                     ASLFR_InitialPattern,    "#?.iff",
  102.                                                     ASLFR_InitialFile,        "picture.iff",
  103.                                                     TAG_DONE)))
  104.     {
  105.         struct Window* win = getDrawWin();
  106.         if(AslRequestTags(savereq, ASLFR_Window, win, TAG_DONE))
  107.         {
  108.             char filename[MAXFILENAME];
  109.             /* Create complete filename from ASL's dir and file */
  110.             strcpy(filename, savereq->rf_Dir);
  111.             if(AddPart(filename, savereq->rf_File, MAXFILENAME))
  112.             {
  113.                 /* Make sure our bitmap is the same as the display */
  114.                 SyncSBitMap(win->WLayer);
  115.                 /* Try saving our bitmap, using the screen's colours */
  116.                 if(IFFL_SaveBitMap(filename, getBitmap(),
  117.                                                     win->WScreen->ViewPort.ColorMap->ColorTable,
  118.                                                     IFFL_COMPR_BYTERUN1) == 0)
  119.                     printf("Error: could not write IFF picture\n");
  120.             }
  121.             else
  122.                 printf("Error: could not make filename\n");
  123.         }
  124.         /* else: requester was cancelled */
  125.     }
  126.     else
  127.         printf("Error: could not allocate ASL (save) file request\n");
  128. }
  129.  
  130. /* Free any requesters that may have been allocated */
  131. void freeReqs()
  132. {
  133.     if(loadreq)
  134.     {
  135.         FreeAslRequest(loadreq);
  136.         loadreq = NULL;
  137.     }
  138.     if(savereq)
  139.     {
  140.         FreeAslRequest(savereq);
  141.         savereq = NULL;
  142.     }
  143. }
  144.  
  145.